
global data /bplimext/projects/p064_MathildeMunoz/initial_dataset
global outputs /bplimext/projects/p064_MathildeMunoz/work_area

global list 33120 33200 41200 42110 42120 42130 42210 42220 42910 42990 ///
43110 43120 43130 43210 43221 43222 43290 43310 43320 43330 43340 43390 43910 43992 49410 78200 

clear 

*********************************************************
* Replication for Table 3 (+ supplementary analysis in Appendix E1 and C2)
********************************************************

*raw data from BPLIM server 

use "$data/PE064_CBHP_A_YFRM_20062017_JUN19_ROSTO_V01.dta", clear
merge 1:1 ano tina using "$data/PE064_CBHP_A_YFRM_20062017_JUN19_MG_V01.dta"
drop _m
merge 1:1 ano tina using "$data/PE064_CBHP_A_YFRM_20062017_JUN19_PESSOAL_V01.dta"
drop _m
g export_service_EU=0
replace export_service_EU=1 if MG005!=0
bys ano: tab export_service_EU
bys ano: tab exporta
g export_all=MG004+MG007
g export_sales_EU=0
replace export_sales_EU=1 if export_all!=0
bys ano: tab export_sales_EU
drop if cae3==31091
g postable=0
foreach y in $list {
replace postable=1 if cae3==`y'
}

merge 1:1 ano tina using "$data/PE064_CBHP_A_YFRM_20062017_JUN19_CONTAS_V01.dta"
drop _m

g tempvar=string(cae3, "%3.0g")
g gp=substr(tempvar,1,1)
destring gp, replace
g manufacturing=0
replace manufacturing=1 if gp==2
replace manufacturing=1 if gp==1
g servca=MG005/D001
g goodsca=MG004/D001

sum E001 if export_sales_EU==1 & manufacturing==1 ,d 
sum E001 if export_service_EU==1 & postable==1 ,d 

sum D001 if export_sales_EU==1 & manufacturing==1 ,d 
sum D001 if export_service_EU==1 & postable==1 ,d 

sum D001 if export_sales_EU==0 & manufacturing==1 ,d 
sum D001 if export_service_EU==0 & postable==1 ,d 

sum B001 if export_sales_EU==1 & manufacturing==1 ,d 
sum B001 if export_service_EU==1 & postable==1 ,d 

g capital_ca=B001/D001
g asset_ca=D001/B001

sum capital_ca if export_sales_EU==1 & manufacturing==1 ,d 
sum capital_ca if export_service_EU==1 & postable==1 ,d 

egen panelexport_service=sum(export_service_EU)
egen panelexport_goods=sum(export_sales_EU)

sum E001 if panelexport_goods>0 & manufacturing==1 ,d 
sum E001 if panelexport_service>0 & postable==1 ,d 

g intangible=B005-B012

 *egen median=median(E001), by(tina)
 * drop if median<3
 * drop if median==.

*Figure C21

sum E001 if export_sales_EU==1 & manufacturing==1 ,d 
global floor=r(p95)
global mean=round(r(mean),0.01)
global median=round(r(p50),0.01)
sum E001 if export_service_EU==1 & postable==1 ,d 
global floor2=r(p95)
global mean2=round(r(mean),0.01)
global median2=round(r(p50),0.01)
twoway (hist E001 if export_sales_EU==1 & manufacturing==1 & E001<200, frac width(5) color(red%30)) ///
(hist E001 if export_service_EU==1 & postable==1 & E001<200, frac width(5) color(eltblue%30)), ///
graphregion(fcolor(white) lcolor(white)) xtitle("Number of Employees") ///
ytitle("Fraction") legend(label(1 "Exporting Firms, Manufacturing") ///
label(2 "Exporting Firms, Non Tradable Services") cols(1)) ///
text( 0.45 100 "Manufacturing" "Average=$mean"  "Median=$median" , justification(left)) ///
text( 0.45 160 "Non Tradable Services" "Average=$mean2"  "Median=$median2" , justification(left)) 
graph export "$outputs/ES/Baseline_Export5/histE001.png", replace

g D001gr=D001/1000
sum D001gr if export_sales_EU==1 & manufacturing==1 ,d 
global floor=r(p95)
global mean=round(r(mean),0.01)
global median=round(r(p50),0.01)
sum D001gr if export_service_EU==1 & postable==1 ,d 
global floor2=r(p95)
global mean2=round(r(mean),0.01)
global median2=round(r(p50),0.01)
twoway (hist D001gr if export_sales_EU==1 & manufacturing==1 & D001gr<5000, frac width(100) color(red%30)) ///
(hist D001gr if export_service_EU==1 & postable==1 & D001gr<5000, frac width(100) color(eltblue%30)), ///
graphregion(fcolor(white) lcolor(white)) xtitle("Turnover (Thousand Euros)") ///
ytitle("Fraction") legend(label(1 "Exporting Firms, Manufacturing") ///
label(2 "Exporting Firms, Non Tradable Services") cols(1)) ///
text( 0.25 2000 "Manufacturing" "Average=$mean"  "Median=$median" , justification(left)) ///
text( 0.25 4000 "Non Tradable Services" "Average=$mean2"  "Median=$median2" , justification(left)) 
graph export "$outputs/ES/Baseline_Export5/histD001.png", replace


sum capital_ca if export_sales_EU==1 & manufacturing==1 & capital_ca<5 & capital_ca>-2,d 
global floor=r(p95)
global mean=round(r(mean),0.01)
global median=round(r(p50),0.01)
sum capital_ca if export_service_EU==1 & postable==1 & capital_ca<5 & capital_ca>-2,d 
global floor2=r(p95)
global mean2=round(r(mean),0.01)
global median2=round(r(p50),0.01)
twoway (hist capital_ca if export_sales_EU==1 & manufacturing==1 & capital_ca<5 & capital_ca>-2, frac width(0.1) color(red%30)) ///
(hist capital_ca if export_service_EU==1 & postable==1 & capital_ca<5 & capital_ca>-2, frac width(0.1) color(eltblue%30)), ///
graphregion(fcolor(white) lcolor(white)) xtitle("Total Assets/Total Sales") ///
ytitle("Fraction") legend(label(1 "Exporting Firms, Manufacturing") ///
label(2 "Exporting Firms, Non Tradable Services") cols(1)) ///
text( 0.09 2 "Manufacturing" "Average=$mean"  "Median=$median" , justification(left)) ///
text( 0.09 4.5 "Non Tradable Services" "Average=$mean2"  "Median=$median2" , justification(left)) 
graph export "$outputs/ES/Baseline_Export5/histcapitalca.png", replace





g B001gr=B001/1000
sum B001gr if export_sales_EU==1 & manufacturing==1 ,d 
global floor=r(p95)
global mean=round(r(mean),0.01)
global median=round(r(p50),0.01)
sum B001gr if export_service_EU==1 & postable==1 ,d 
global floor2=r(p95)
global mean2=round(r(mean),0.01)
global median2=round(r(p50),0.01)
twoway (hist B001gr if export_sales_EU==1 & manufacturing==1 & B001gr<5000, frac width(100) color(red%30)) ///
(hist B001gr if export_service_EU==1 & postable==1 & B001gr<5000, frac width(100) color(eltblue%30)), ///
graphregion(fcolor(white) lcolor(white)) xtitle("Net Assets (Thousand Euros)") ///
ytitle("Fraction") legend(label(1 "Exporting Firms, Manufacturing") ///
label(2 "Exporting Firms, Non Tradable Services") cols(1)) ///
text( 0.25 2000 "Manufacturing" "Average=$mean"  "Median=$median" , justification(left)) ///
text( 0.25 4000 "Non Tradable Services" "Average=$mean2"  "Median=$median2" , justification(left)) 
graph export "$outputs/ES/Baseline_Export5/histB001.png", replace



g intangiblegr=intangible/1000
sum intangiblegr if export_sales_EU==1 & manufacturing==1 ,d 
global floor=r(p95)
global mean=round(r(mean),0.01)
global median=round(r(p50),0.01)
sum intangiblegr if export_service_EU==1 & postable==1 ,d 
global floor2=r(p95)
global mean2=round(r(mean),0.01)
global median2=round(r(p50),0.01)
twoway (hist intangiblegr if export_sales_EU==1 & manufacturing==1 & intangiblegr<500, frac width(10) color(red%30)) ///
(hist intangiblegr if export_service_EU==1 & postable==1 & intangiblegr<500, frac width(10) color(eltblue%30)), ///
graphregion(fcolor(white) lcolor(white)) xtitle("Intangible Assets (Thousand Euros)") ///
ytitle("Fraction") legend(label(1 "Exporting Firms, Manufacturing") ///
label(2 "Exporting Firms, Non Tradable Services") cols(1)) ///
text( 0.25 2000 "Manufacturing" "Average=$mean"  "Median=$median" , justification(left)) ///
text( 0.25 4000 "Non Tradable Services" "Average=$mean2"  "Median=$median2" , justification(left)) 
graph export "$outputs/ES/Baseline_Export5/histintangible.png", replace




g type="."
replace type="postable" if postable==1
replace type="manufacturing" if manufacturing==1 
g mshare_s=MG005/D001
g mshare_g=MG004/D001
g freq=1
g posti=0
replace posti=1 if MG005>0
g posti_g=0
replace posti_g=1 if MG004>0

preserve 

collapse (sum) MG005 D001 MG004 B001 E001 freq posti* exporta (mean) mshare*, by(ano type)

g share_s=MG005/D001
g share_g=MG004/D001

twoway (connected share_s ano if type=="postable") (connected share_g ano if type=="postable") ///


twoway (connected share_s ano if type=="postable" & ano<2017, lcolor(cranberry) mcolor(cranberry)) ///
(connected share_g ano if type=="postable" & ano<2017, lcolor(dkanvy) mcolor(dkanvy)), ///
graphregion(fcolor(white) lcolor(white)) ///
xtitle("") ylabel(0"0%" 0.05"5%" 0.15"15%") ytitle("Percent in Total Turnover") ///
xlabel(1988 (1) 2016, angle(vertical)) legend(label(1 "Services Exports") label(2 "Goods Exports") cols(1))
graph export "$outputs/ES/Baseline_Export5/postable_services.png", replace


twoway (connected share_s ano if type=="manufacturing" & ano<2017, lcolor(cranberry) mcolor(cranberry)) ///
(connected share_g ano if type=="manufacturing" & ano<2017, lcolor(dkanvy) mcolor(dkanvy)), ///
graphregion(fcolor(white) lcolor(white)) ///
xtitle("") ylabel(0"0%" 0.15"15%" 0.3 "30%") ytitle("Percent in Total Turnover") ///
xlabel(1988 (1) 2016, angle(vertical)) legend(label(1 "Services Exports") label(2 "Goods Exports") cols(1))
graph export "$outputs/ES/Baseline_Export5/manuf_services.png", replace

twoway (connected mshare_s ano if type=="postable" & ano<2017, lcolor(cranberry) mcolor(cranberry)) ///
(connected mshare_g ano if type=="postable" & ano<2017, lcolor(dkanvy) mcolor(dkanvy)), ///
graphregion(fcolor(white) lcolor(white)) ///
xtitle("") ylabel(0"0%" 0.05"5%" 0.1"10%") ytitle("Average Percent in Turnover") ///
xlabel(1988 (1) 2016, angle(vertical)) legend(label(1 "Services Exports") label(2 "Goods Exports") cols(1))
graph export "$outputs/ES/Baseline_Export5/postable_services_av.png", replace


twoway (connected mshare_s ano if type=="manufacturing" & ano<2017, lcolor(cranberry) mcolor(cranberry)) ///
(connected mshare_g ano if type=="manufacturing" & ano<2017, lcolor(dkanvy) mcolor(dkanvy)), ///
graphregion(fcolor(white) lcolor(white)) ///
xtitle("") ylabel(0"0%" 0.05"5%" 0.1"10%") ytitle("Average Percent in  Turnover") ///
xlabel(1988 (1) 2016, angle(vertical)) legend(label(1 "Services Exports") label(2 "Goods Exports") cols(1))
graph export "$outputs/ES/Baseline_Export5/manuf_services_av.png", replace




g frac_s=posti/freq
g frac_g=posti_g/freq
g frac=exporta/freq

twoway (connected frac_s ano if type=="postable" & ano<2017, lcolor(cranberry) mcolor(cranberry)) ///
(connected frac_g ano if type=="manufacturing" & ano<2017, lcolor(dkanvy) mcolor(dkanvy)), ///
graphregion(fcolor(white) lcolor(white)) ///
xtitle("") ylabel(0"0%" 0.15"15%" ) ytitle("% of Exporting Firms (Within EU)") ///
xlabel(1988 (1) 2016, angle(vertical)) legend(label(1 "Services Exports, Non Tradable Services") ///
label(2 "Goods Exports, Manufacturing") cols(1))
graph export "$outputs/ES/Baseline_Export5/nbfirms.png", replace

restore
preserve 
*top export goods
g topg=.
g tops=.
forval y=2006/2017{
su MG004 if manufacturing==1 & ano==`y',d 
global floor=r(p99)
replace topg=1 if ano==`y' & manufacturing==1 & MG004>$floor
}
forval y=2006/2017{
su MG005 if postable==1 & ano==`y',d 
global floor=r(p99)
replace tops=1 if ano==`y' & postable==1 & MG005>$floor
}

egen tot_s=sum(MG005), by(ano type)
egen tot_g=sum(MG004), by(ano type)
egen tot_ca=sum(D001), by(ano type)

collapse (sum) MG005 MG004 D001 (mean) tot*, by(ano  tops type topg)

 g testshare=tot_s/tot_ca
 g share_s=MG005/tot_s
 g share_g=MG004/tot_g

 twoway (connected share_s ano if type=="postable" & tops==1 & ano<2017, lcolor(cranberry) mcolor(cranberry)) ///
 (connected share_g ano if type=="manufacturing" & topg==1 & ano<2017, lcolor(black) mcolor(black)), ///
 ylabel(0.5"50%" 0.7"70%" 1"100%") xlabel(1989 (1) 2016, angle(vertical)) graphregion(fcolor(white) lcolor(white)) ///
 ytitle("% of Total Exports") ///
 legend(label(1 "Top 1%  Exporters, Non Tradable Services") label(2 "Top 1% Exporters, Manufacturing") cols(1))
 graph export "$outputs/ES/Baseline_Export5/bigexporters.png", replace
 
 restore
 
 
 ******************************************************************************
 * Event-study for manufacturing firms: Panel C Table 3 + appendix Figure E69 
 ******************************************************************************
 
global data /bplimext/projects/p064_MathildeMunoz/initial_dataset
global outputs /bplimext/projects/p064_MathildeMunoz/work_area

global list 33120 33200 41200 42110 42120 42130 42210 42220 42910 42990 ///
43110 43120 43130 43210 43221 43222 43290 43310 43320 43330 43340 43390 43910 43992 49410 78200 

clear 

use "$data/PE064_CBHP_A_YFRM_20062017_JUN19_ROSTO_V01.dta", clear
merge 1:1 ano tina using "$data/PE064_CBHP_A_YFRM_20062017_JUN19_MG_V01.dta"
drop _m
merge 1:1 ano tina using "$data/PE064_CBHP_A_YFRM_20062017_JUN19_PESSOAL_V01.dta"
drop _m
g export_service_EU=0
replace export_service_EU=1 if MG005!=0
bys ano: tab export_service_EU
bys ano: tab exporta
g export_all=MG004+MG007
g export_sales_EU=0
replace export_sales_EU=1 if export_all!=0 
g postable=0
foreach y in $list {
replace postable=1 if cae3==`y'
}

g tempvar=string(cae3, "%3.0g")
g gp=substr(tempvar,1,1)
destring gp, replace

*Identify manufacturing firms using code nace 

g manufacturing=0
replace manufacturing=1 if gp==2
replace manufacturing=1 if gp==1


g flag=1
egen tot=sum(flag), by(ano cae3)
egen panel_export=sum(export_service_EU), by(tina)
egen panel=sum(flag), by(tina)

count if postable==1 & panel_export>=1
merge 1:1 ano tina using "$data/PE064_CBHP_A_YFRM_20062017_JUN19_CONTAS_V01.dta"
drop _m

*Keep only manufacturing firms (with and without exports)

keep if manufacturing==1
drop panel_export 
egen panel_export=sum(export_sales_EU), by(tina)

sum E002 if panel_export==0, d 
sum E002 if panel_export>=1, d 
sum E001 if panel_export==0, d 
sum E001 if panel_export>=1, d 
g wage_cap=D029/E012

*Event of exporting goods

forval y=2006/2017{
g dum_`y'=0
replace dum_`y'=1 if ano==`y' & export_sales_EU==1
egen dumi_`y'=max(dum_`y'), by(tina)
}



bysort tina (ano):gen cum=sum(export_sales_EU)
g event=ano if cum==1 & export_sales_EU==1
egen event_d=min(event), by(tina)
replace event_d=0 if missing(event_d)
g treated=0
replace treated=1 if panel_export>=1

g time=ano-event_d
replace time=0 if treated==0 & panel_export==0
g timed=time+11 if treated==1
replace timed=10 if treated==0

forval y=0/22{
g coeff_`y'=0
replace coeff_`y'=1 if timed==`y'
}

g alert=0
replace alert=1 if time>0 & export_sales_EU==0
g zero=1
label var zero "-1"

forval y=0/22{
local j=`y'-11
label var coeff_`y' "`j'"
}

egen cluster=group(cae3 distrito)
save "$outputs/ES/paneldata_manufacturing.dta", replace

********************************************************
* Export vs posting gains: Table 3 and appendix figures
********************************************************
 

*Append event-study panel on posting firms + manufacturing firms 

use "$outputs/ES/paneldata_ES.dta", clear
append using "$outputs/ES/paneldata_manufacturing.dta"

g logemp=log(E001)
g logca=log(D001)
g logassets=log(B001)
g logwage=log(wage_cap)
g logcapca=log(B001/E001)
g loginputs=log(D025)
g logassetsturnover=log(B001/D001)
g prof=log(D086/D001)
g vaperw=log(D001-D025)-log(E001)
g payrollca=log(D029/D001)
g logEBT=log(D086)
g logEBTworker=log(D086/E001)
g EBTmargin=log(D086/D001)
g EBTmarginr=D086/D001

foreach y in logca logemp logassets logwage logcapca loginputs logassetsturnover prof {
winsor `y', p(.01) gen(`y'_w)
}

foreach y in vaperw {
winsor `y', p(.01) gen(`y'_w)
}

foreach y in payrollca {
winsor `y', p(.01) gen(`y'_w)
}

*careful this command is very long to run -- comment out if computational constraints! 
acfest logca, free(logemp) state(cae3) proxy(loginputs) i(tina) t(ano)
predict myomega, omega 
replace manufacturing=0 if missing(manufacturing)

*Comparison of importing premium in each sectors-- Tab 3 Panel B 

reghdfe logemp_w i.treated if postable==1 , absorb(i.ano##i.distrito##i.cae3) cl(tina)
reghdfe logemp_w i.treated if manufacturing==1, absorb(i.ano##i.distrito##i.cae3) cl(tina)

reghdfe logca_w i.treated if postable==1 , absorb(i.ano##i.distrito##i.cae3) cl(tina)
reghdfe logca_w i.treated if manufacturing==1, absorb(i.ano##i.distrito##i.cae3) cl(tina)


reghdfe logwage_w i.treated if postable==1 , absorb(i.ano##i.distrito##i.cae3) cl(tina)
reghdfe logwage_w i.treated if manufacturing==1, absorb(i.ano##i.distrito##i.cae3) cl(tina)


reghdfe logcapca_w i.treated if postable==1 , absorb(i.ano##i.distrito##i.cae3) cl(tina)
reghdfe logcapca_w i.treated if manufacturing==1, absorb(i.ano##i.distrito##i.cae3) cl(tina)

reghdfe logassetsturnover_w i.treated if postable==1 , absorb(i.ano##i.distrito##i.cae3) cl(tina)
reghdfe logassetsturnover_w i.treated if manufacturing==1, absorb(i.ano##i.distrito##i.cae3) cl(tina)

reghdfe prof_w i.treated if postable==1 , absorb(i.ano##i.distrito##i.cae3) cl(tina)
reghdfe prof_w i.treated if manufacturing==1, absorb(i.ano##i.distrito##i.cae3) cl(tina)

reghdfe vaperw_w i.treated if postable==1 , absorb(i.ano##i.distrito##i.cae3) cl(tina)
reghdfe vaperw_w i.treated if manufacturing==1, absorb(i.ano##i.distrito##i.cae3) cl(tina)

reghdfe payrollca_w i.treated if postable==1 , absorb(i.ano##i.distrito##i.cae3) cl(tina)
reghdfe payrollca_w i.treated if manufacturing==1, absorb(i.ano##i.distrito##i.cae3) cl(tina)

reghdfe logEBT i.treated if postable==1 , absorb(i.ano##i.distrito##i.cae3) cl(tina)
reghdfe logEBT i.treated if manufacturing==1, absorb(i.ano##i.distrito##i.cae3) cl(tina)

reghdfe logEBTworker i.treated if postable==1 , absorb(i.ano##i.distrito##i.cae3) cl(tina)
reghdfe logEBTworker i.treated if manufacturing==1, absorb(i.ano##i.distrito##i.cae3) cl(tina)

reghdfe EBTmargin i.treated if postable==1 , absorb(i.ano##i.distrito##i.cae3) cl(tina)
reghdfe EBTmargin i.treated if manufacturing==1, absorb(i.ano##i.distrito##i.cae3) cl(tina)




reghdfe logca_w i.manufacturing if treated==1, absorb(i.ano##i.distrito) cl(tina)
reghdfe logwage_w i.manufacturing if treated==1, absorb(i.ano##i.distrito) cl(tina)
reghdfe logemp_w i.manufacturing if treated==1, absorb(i.ano##i.distrito) cl(tina)
reghdfe logcapca_w i.manufacturing if treated==1, absorb(i.ano##i.distrito) cl(tina)
reghdfe prof_w i.manufacturing if treated==1, absorb(i.ano##i.distrito) cl(tina)
reghdfe vaperw_w i.manufacturing if treated==1, absorb(i.ano##i.distrito) cl(tina)
reghdfe payrollca_w i.manufacturing if treated==1, absorb(i.ano##i.distrito) cl(tina)


reghdfe logEBT i.manufacturing if treated==1, absorb(i.ano##i.distrito) cl(tina)
reghdfe logEBTworker i.manufacturing if treated==1, absorb(i.ano##i.distrito) cl(tina)

reghdfe EBTmargin i.manufacturing if treated==1, absorb(i.ano##i.distrito) cl(tina)


*pre export self-selection: Panel A
 
g t2009=0
replace t2009=1 if event_d==2010 & treated==1
replace t2009=1 if treated==0


*duration of exports 

g type="."
replace type="postable" if postable==1
replace type="manufacturing" if manufacturing==1
preserve 

keep if ano==2007  & event_d==2007
g freq=1
egen toti=sum(freq), by(type)
collapse (sum) freq (mean) toti , by(ano type panel_export)
g share=freq/toti
egen test=sum(share), by(type)
keep panel_export type share
reshape wide share, i(panel_export) j(type) string

*Figure c19 

graph bar sharemanufacturing sharepostable, over(panel_export) ///
graphregion(fcolor(white) lcolor(white)) legend(label(1 "Manufacturing") label(2 "Non Tradable Services") cols(1)) ///
ylabel(0.1 "10%" 0.2"20%" 0.3"30%") b1title("Years of Exports")
 graph export "$outputs/ES/Baseline_Export5/export2007duration.png", replace


restore

g export=.
replace export=MG005 if postable==1
replace export=export_all if manufacturing==1
g share=export/D001

preserve 

*same sample restrictions than for baseline 
keep if treated==1
drop if event_d<2010 & treated==1
drop if event_d>2015 & treated==1
egen median=median(E001), by(tina)
drop if median<3
drop if median==.
collapse (mean) share, by(time type)

*First stage in manufacturing vs posting 
*Figure C22 

 twoway (connected share time if type=="postable") ///
 (connected share time if type=="manufacturing"), ///
  graphregion(fcolor(white) lcolor(white)) ///
  xlabel(-9  (1) 7) xline(-0.5, lcolor(red)) ///
  xtitle("Years to First Export") ytitle("% in Total Turnover") ///
  legend(label(1 "Services Exports, Non Tradables") label(2 "Goods Exports, Manufacturing") cols(1)) ///
  ylabel(0.1 "10%" 0.2"20%" 0.3"30%" 0.4"40%")
 graph export "$outputs/ES/Baseline_Export5/treatmentintensity.png", replace
restore 


*Summary statistics on exporters in manufacturing and posting services 

count if treated==1 & time==0 & postable==1
count if treated==1 & time==0 & postable==1 &  share==1

count if treated==1 & time==0 & manufacturing==1 
count if treated==1 & time==0 & manufacturing==1  & share==1

tab treated if postable==1
tab treated if postable==1


count if ancon==event_d & postable==1
count if ancon==event_d & postable==1


g selfemployed=0
replace selfemployed=1 if E001==0

count if treated==1 & time==0 & manufacturing==1  
count if treated==1 & time==0 & postable==1  

count if treated==1 & time==0 & manufacturing==1   & selfemployed==1
count if treated==1 & time==0 & postable==1   & selfemployed==1

sum share if treated==1 & time==0 & manufacturing==1  
sum share if treated==1 & time==0 & postable==1  

tab panel_export if postable==1 & time==0 & event_d==2007
tab panel_export if manufacturing==1 & time==0 & event_d==2007

g frac=panel_export/panel

sum panel_export if postable==1 & time==0 & treated==1
sum panel_export if manufacturing==1 & time==0 & treated==1

sum frac if postable==1 & time==0 & treated==1
sum frac if manufacturing==1 & time==0 & treated==1

*****************************


g coeff_up=0
replace coeff_up=1 if treated==1 & time>6
g coeff_bottom=0
replace coeff_bottom=1 if treated==1 & time<-4

label var coeff_up ">6"
g coeff_up2=0
replace coeff_up2=1 if time>4 & treated==1
label var coeff_up2 ">4"

egen cluster1=group(ano distrito)
g estim=0
replace estim=1 if event_d>2009 & event_d<2016 & treated==1

*******************************************************************



   egen median=median(E001), by(tina)
   drop if median<3
   drop if median==.
   
   egen clusterb1=group(time distrito)
   
   g logwagebill=log(D029)
   g loghours=log(E012)
   g wage_employees=DL012/E012
*g logwage=log(wage_employees)

g export_other=0
replace export_other=1 if MG008>0
egen panel_export_other=sum(export_other), by(tina)
g alt_treat=0
replace alt_treat=1 if panel_export_other>=1
drop if alt_treat==1 & postable==1


*ESTIMATION OF FIRST EXPORT GAINS: MANUF VS POSTING 
*Tab 3, Panel C + Figure E69 

reghdfe logwage coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="postable" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store wage_postable
reghdfe logwage coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="manufacturing" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store wage_manufacturing
coefplot  (wage_postable, label("Non Tradable Services")) (wage_manufacturing, label("Manufacturing") )   ///
, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(ring(0) pos(11) cols(1)) xtitle("Year to First Export")
graph export "$outputs/ES/Baseline_Export5/Figures/logwage_comparison.png", replace


reghdfe logca coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="postable" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store ca_postable
reghdfe logca coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="manufacturing" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store ca_manufacturing
coefplot  (ca_postable, label("Non Tradable Services")) (ca_manufacturing, label("Manufacturing") )   ///
, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(ring(0) pos(11) cols(1)) xtitle("Year to First Export")
graph export "$outputs/ES/Baseline_Export5/Figures/logca_comparison.png", replace


reghdfe logassets coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="postable" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store assets_postable
reghdfe logassets coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="manufacturing" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store assets_manufacturing
coefplot  (assets_postable, label("Non Tradable Services")) (assets_manufacturing, label("Manufacturing") )   ///
, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(ring(0) pos(11) cols(1)) xtitle("Year to First Export")
graph export "$outputs/ES/Baseline_Export5/Figures/logassets_comparison.png", replace


reghdfe logemp coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="postable" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store emp_postable
reghdfe logemp coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="manufacturing" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store emp_manufacturing
coefplot  (emp_postable, label("Non Tradable Services")) (emp_manufacturing, label("Manufacturing") )   ///
, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(ring(0) pos(11) cols(1)) xtitle("Year to First Export")
graph export "$outputs/ES/Baseline_Export5/Figures/logemp_comparison.png", replace


g logprofit=log(D086)

reghdfe logprofit coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="postable" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store profit_postable
reghdfe logprofit coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="manufacturing" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store profit_manufacturing
coefplot  (profit_postable, label("Non Tradable Services")) (profit_manufacturing, label("Manufacturing") )   ///
, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(ring(0) pos(11) cols(1)) xtitle("Year to First Export")
graph export "$outputs/ES/Baseline_Export5/Figures/logprofit_comparison.png", replace

g logincome=log(D087)


reghdfe logincome coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="postable" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store income_postable
reghdfe logincome coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="manufacturing" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store income_manufacturing
coefplot  (income_postable, label("Non Tradable Services")) (income_manufacturing, label("Manufacturing") )   ///
, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(ring(0) pos(11) cols(1)) xtitle("Year to First Export")
graph export "$outputs/ES/Baseline_Export5/Figures/logincome_comparison.png", replace


g logtangible=log(B012)


reghdfe logtangible coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="postable" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store tangible_postable
reghdfe logtangible coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="manufacturing" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store tangible_manufacturing
coefplot  (tangible_postable, label("Non Tradable Services")) (tangible_manufacturing, label("Manufacturing") )   ///
, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(ring(0) pos(11) cols(1)) xtitle("Year to First Export")
graph export "$outputs/ES/Baseline_Export5/Figures/logtangible_comparison.png", replace


g logtangible_b=log(1+B012)


reghdfe logtangible_b coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="postable" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store tangibleb_postable
reghdfe logtangible_b coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="manufacturing" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store tangibleb_manufacturing
coefplot  (tangibleb_postable, label("Non Tradable Services")) (tangibleb_manufacturing, label("Manufacturing") )   ///
, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(ring(0) pos(11) cols(1)) xtitle("Year to First Export")
graph export "$outputs/ES/Baseline_Export5/Figures/logtangibleb_comparison.png", replace


g loginternal=log(MG001+MG002)


reghdfe loginternal coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="postable" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store internal_postable
reghdfe loginternal coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="manufacturing" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store internal_manufacturing
coefplot  (internal_postable, label("Non Tradable Services")) (internal_manufacturing, label("Manufacturing") )   ///
, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(ring(0) pos(11) cols(1)) xtitle("Year to First Export")
graph export "$outputs/ES/Baseline_Export5/Figures/loginternal_comparison.png", replace


g loginternalb=log(1+MG001+MG002)


reghdfe loginternalb coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="postable" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store internalb_postable
reghdfe loginternalb coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="manufacturing" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store internalb_manufacturing
coefplot  (internalb_postable, label("Non Tradable Services")) (internalb_manufacturing, label("Manufacturing") )   ///
, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(ring(0) pos(11) cols(1)) xtitle("Year to First Export")
graph export "$outputs/ES/Baseline_Export5/Figures/loginternalb_comparison.png", replace


g loginternalb2=asinh(MG001+MG002)


reghdfe loginternalb2 coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="postable" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store internalb2_postable
reghdfe loginternalb2 coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="manufacturing" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store internalb2_manufacturing
coefplot  (internalb2_postable, label("Non Tradable Services")) (internalb2_manufacturing, label("Manufacturing") )   ///
, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.8 (.4) .8) ///
xline(4.5, lcolor(red)) scale(1.2) legend(ring(0) pos(11) cols(1)) xtitle("Year to First Export")
graph export "$outputs/ES/Baseline_Export5/Figures/loginternalb2_comparison.png", replace


reghdfe logwagebill coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="postable" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store wagebill_postable
reghdfe logwagebill coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="manufacturing" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store wagebill_manufacturing
coefplot  (wagebill_postable, label("Non Tradable Services")) (wagebill_manufacturing, label("Manufacturing") )   ///
, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(ring(0) pos(11) cols(1)) xtitle("Year to First Export")
graph export "$outputs/ES/Baseline_Export5/Figures/logwagebill_comparison.png", replace


reghdfe loghours coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="postable" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store hours_postable
reghdfe loghours coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="manufacturing" & estim==1 , absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store hours_manufacturing
coefplot  (hours_postable, label("Non Tradable Services")) (wagebill_manufacturing, label("Manufacturing") )   ///
, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2) legend(ring(0) pos(11) cols(1)) xtitle("Year to First Export")
graph export "$outputs/ES/Baseline_Export5/Figures/loghours_comparison.png", replace

g wage_emp=DL012/E012
g logwageemp=log(wage_emp)
g minwage_indic=0
replace minwage_indic=1 if wage_emp>12 & wage_emp!=.
g indicwage=0
replace indicwage=1 if minwage_indic==1 & time==-1
egen nocons=max(indicwage), by(tina)

reghdfe logwage coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="postable" & estim==1 & nocons==1, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store wagenocons
reghdfe logemp coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if treated==1 & type=="postable" & estim==1 & nocons==1, absorb( i.tina i.cae3 i.ano i.distrito i.ano##i.distrito##i.cae3) cl(clusterb1)
est store empnocons

***********************************************************************************
* More Descriptive statistics on exports by sectors 
***********************************************************************************



global list 33120 33200 41200 42110 42120 42130 42210 42220 42910 42990 ///
43110 43120 43130 43210 43221 43222 43290 43310 43320 43330 43340 43390 43910 43992 49410 78200 

clear 

use "$data/PE064_CBHP_A_YFRM_20062017_JUN19_ROSTO_V01.dta", clear
merge 1:1 ano tina using "$data/PE064_CBHP_A_YFRM_20062017_JUN19_MG_V01.dta"
drop _m
merge 1:1 ano tina using "$data/PE064_CBHP_A_YFRM_20062017_JUN19_PESSOAL_V01.dta"
drop _m
g export_service_EU=0
replace export_service_EU=1 if MG005!=0
bys ano: tab export_service_EU
bys ano: tab exporta
g export_all=MG004+MG007
g export_sales_EU=0
replace export_sales_EU=1 if export_all!=0
bys ano: tab export_sales_EU
drop if cae3==31091

g postable=0
foreach y in $list {
replace postable=1 if cae3==`y'
}

merge 1:1 ano tina using "$data/PE064_CBHP_A_YFRM_20062017_JUN19_CONTAS_V01.dta"
drop _m

g tempvar=string(cae3, "%3.0g")
g gp=substr(tempvar,1,1)
destring gp, replace
g manufacturing=0
replace manufacturing=1 if gp==2
replace manufacturing=1 if gp==1
g servca=MG005/D001
g goodsca=MG004/D001

keep if ano==2015
g freq=1
egen tot_firm=sum(freq)
egen tot_emp=sum(E001)
egen tot_ca=sum(D001) 
g serv=MG005/D001 if MG005>0
g sales=export_all/D001 if export_all>0
preserve 
collapse (sum) freq export_service_EU export_sales_EU MG005 export_all E001 D001 (mean) tot* postable manufacturing serv sales, by(cae3 )

g s_service=export_service_EU/freq
g s_sales=export_sales_EU/freq
g s_firms=freq/tot_firm
g s_emploi=E001/tot_emp
g s_turnvover=D001/tot_ca
restore

g type="."
replace type="manufacturing" if manufacturing==1
replace type="postable" if postable==1

collapse (sum) freq export_service_EU export_sales_EU MG005 export_all E001 D001 (mean) tot* postable manufacturing serv sales, by(type )

g s_service=export_service_EU/freq
g s_sales=export_sales_EU/freq
g s_firms=freq/tot_firm
g s_emploi=E001/tot_emp
g s_turnvover=D001/tot_ca


